package com.logic.modular.act.match;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.comment.util.message.ResponseResult;
import com.logic.comment.util.JwtUtils;
import com.logic.modular.act.battle.vo.BattleVo;
import com.logic.modular.game.core.domain.PvpMatchEnd;
import com.logic.modular.game.core.service.MatchEndServiceImpl;
import com.logic.modular.game.alliance.service.AllianceServiceImpl;
import com.comment.pojo.domain.logic.domain.Player;
import com.comment.pojo.domain.logic.domain.PlayerStatus;
import com.logic.modular.game.player.info.service.PlayerServiceImpl;
import com.comment.pojo.domain.logic.core.PvpMatch;
import com.logic.modular.game.player.info.service.PlayerStatusServiceImpl;
import com.logic.modular.game.core.service.MatchServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Api(tags = "pvp匹配")
@Validated
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/pvpMatchAct")
public class PvpMatchAction {
    @Resource
    private MatchServiceImpl pvpMatchService;
    @Resource
    private MatchEndServiceImpl pvpMatchEndService;
    @Resource
    private PlayerStatusServiceImpl playerMatchServiceImpl;
    @Resource
    private PlayerServiceImpl playerServiceImpl;
    @Resource
    private MatchServiceImpl pvpMatchServiceImpl;
    @Resource
    private AllianceServiceImpl allianceService;

    @ApiOperation(value = "根据对局id查询对局")
    @PostMapping("/select")
    public ResponseResult<PvpMatch> select(
            @Valid @RequestBody BattleVo.pvpMatchVo.select param
    ) {
        return ResponseResult.success("成功匹配对局",
                pvpMatchService.getById(param.getMatchId())
        );
    }

    @ApiOperation(value = "快速匹配")
    @PostMapping("/realPersonMatchQuick")
    public ResponseResult<?> realPersonMatchQuick(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.pvpMatchVo.realPersonMatchQuick param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Integer mapId = param.getMapId();
        Integer startThreshold = param.getStartThreshold();
        pvpMatchService.realPersonMatchQuick(playerId, Long.valueOf(mapId), startThreshold);
        return ResponseResult.success("快速匹配成功");
    }

    @ApiOperation(value = "人机匹配")
    @PostMapping("/manMachineMatch")
    public ResponseResult<PvpMatch> manMachineMatch(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.pvpMatchVo.manMachineMatch param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long mapId = param.getMapId();
        Integer startThreshold = param.getStartThreshold();
        return ResponseResult.success("成功获取匹配",
                pvpMatchService.manMachineMatch(playerId, mapId, startThreshold)
        );
    }

    @ApiOperation(value = "取消匹配")
    @PostMapping("/cancelMatch")
    public ResponseResult<PvpMatch> cancelMatch(
            @RequestHeader String token
    ) {
        return ResponseResult.success("成功取消对局", pvpMatchService.cancelMatch(
                JwtUtils.getPlayerId(token))
        );
    }

    @ApiOperation(value = "投降")
    @PostMapping("/exitGame")
    public ResponseResult<?> exitGame(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long currentMatchId = playerMatchServiceImpl.currentMatchId(playerId);
        pvpMatchEndService.exitGame(playerId, currentMatchId, 1);
        return ResponseResult.success("成功退出对局");
    }

    @ApiOperation(value = "参加对局的玩家")
    @PostMapping("/allPlayers")
    public ResponseResult<List<Player>> playersParticipatingInTheGame(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long currentMatchId = playerMatchServiceImpl.currentMatchId(playerId);
        PvpMatch byId = pvpMatchServiceImpl.getById(currentMatchId);
        Set<Long> playerCount = byId.getPlayerCount();
        List<Player> collect = playerCount.
                stream().map(item -> playerServiceImpl.select(item)).collect(Collectors.toList());
        return ResponseResult.success("成功参加对局的玩家", collect);
    }

    @ApiOperation(value = "当前对局存活的玩家")
    @PostMapping("/survivingPlayers")
    public ResponseResult<List<Player>> currentSurvivingPlayersInTheGame(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        PlayerStatus playerMatch = playerMatchServiceImpl.selectBy(playerId);
        PvpMatch byId = pvpMatchServiceImpl.getById(playerMatch.getCurrentMatchId());
        Integer status = byId.getStatus();
        if (status != 1) {
            throw new RuntimeException("当前对局未进行");
        }
        List<Player> collect =
                byId.getPlayerIds().stream().map(item -> playerServiceImpl.select(item)).collect(Collectors.toList());
        return ResponseResult.success("成功当前对局存活的玩家", collect);
    }


    @ApiOperation(value = "结盟面板")
    @PostMapping("/alliancePanel")
    public ResponseResult<?> alliancePanel(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        return ResponseResult.success(allianceService.alliancePanel(playerId));
    }

    @ApiOperation(value = "离开联盟")
    @PostMapping("/leaveTheAlliance")
    public ResponseResult<?> leaveTheAlliance(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        allianceService.leaveTheAlliance(playerId);
        return ResponseResult.success();
    }

    @ApiOperation(value = "拒绝指定的联盟邀请")
    @PostMapping("/refuse")
    public ResponseResult<?> refuse(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.pvpMatchVo.refuse param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        allianceService.refuse(playerId, param.getNo());
        return ResponseResult.success();
    }


    @ApiOperation(value = "尝试结盟")
    @PostMapping("/tryAlliance")
    public ResponseResult<?> tryAlliance(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.pvpMatchVo.tryAlliance param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        allianceService.tryAlliance(playerId, param.getPlayerId());
        return ResponseResult.success("已尝试结盟");
    }


    @ApiOperation(value = "比赛消息")
    @PostMapping("/listMatchEnd")
    public ResponseResult<List<PvpMatchEnd>> listMatchEnd(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.pvpMatchVo.listMatchEnd param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        return ResponseResult.success(pvpMatchEndService.list(
                new LambdaQueryWrapper<PvpMatchEnd>()
                        .eq(PvpMatchEnd::getPlayerId, playerId)
                        .eq(param.getMatchId() != null, PvpMatchEnd::getMatchId, param.getMatchId())
        ));
    }
}
